package com.jvbc.xoso.db;

import java.util.ArrayList;
import java.util.List;

import com.jvbc.xoso.bean.DanhMuc;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DanhMucDataSource {

	private SQLiteDatabase db;
	private DataBaseHelper helper;
	
	
	public DanhMucDataSource(Context context){
		helper = new DataBaseHelper(context);
	}
	
	// Them moi
	public void themDanhMuc(DanhMuc danhmuc) {
		db = helper.getWritableDatabase();	
		ContentValues values = new ContentValues(); 
		values.put(DataBaseHelper.KEY_IDBAO, danhmuc.getBaoID()); 
		values.put(DataBaseHelper.KEY_MUC, danhmuc.getMuc()); 
		values.put(DataBaseHelper.KEY_LINK, danhmuc.getLink()); 
		
		// Inserting Row
		db.insert(DataBaseHelper.TABLE_DANHMUC, null, values);
		
		db.close(); // Closing database connection
	}
	// Getting All danh muc
	public List<DanhMuc> getAllDanhMuc(int id) {
		List<DanhMuc> listdanhmuc = new ArrayList<DanhMuc>();
		// Select All Query
		String selectQuery = "SELECT * FROM danhmuc WHERE BaoID ="+id +" ORDER BY Muc";
					
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);
		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				DanhMuc danhmuc = new DanhMuc();
				danhmuc.setId(Integer.parseInt(cursor.getString(0)));
				danhmuc.setBaoID(Integer.parseInt(cursor.getString(1)));
				danhmuc.setMuc(cursor.getString(2));
				danhmuc.setLink(cursor.getString(3));
				// Adding taxi to list
				listdanhmuc.add(danhmuc);
			} while (cursor.moveToNext());
		}
		// close inserting data from database
		db.close();
		// return taxi list
		return listdanhmuc;
			
	}
	// Getting single 
	public	DanhMuc getSingleCategory(int page,String name) {
		SQLiteDatabase db = helper.getReadableDatabase();
		String selectQuery = "SELECT * FROM danhmuc where BaoID ="+page+"and Muc like '%" + name + "%'";	
		Cursor cursor = db.rawQuery(selectQuery, null);
		if (cursor != null)
			cursor.moveToFirst();
					
		DanhMuc danhmuc= new DanhMuc(Integer.parseInt(cursor.getString(0)),Integer.parseInt(cursor.getString(1)),cursor.getString(2),cursor.getString(3));
		db.close();
		return danhmuc;
				
	}
		
}
